<?php
namespace app\common\service;

use app\common\model\MoneyLog as MoneyLogModel;
use app\common\model\UserMoneyLog as UserMoneyLogModel;
use think\Loader;
use think\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/7
 * Time: 15:23
 */
class UserMoneyLog extends Base{
    public $model;
    public function __construct()
    {
        $this->model = new MoneyLogModel();
    }

    /**
     * 提现操作
     * @param $data
     * @param $uid
     * @return array
     */
    public function getDeposit($data,$uid){
        $validate =  Loader::validate('UserMoney');
        if(!$validate->check($data)){
            $datas = json_encode((object)null);
            return ['code'=>3,'msg'=>$validate->getError(),'data'=>$datas];
        }
        $model = $this->model;
        $data['createtime']=time();
        $_datas = $model->insertGetId($data);
        if($_datas){
            $data['id']=$_datas;
            return ['code'=>1,'msg'=>'提现申请成功','data'=>$data];
        }

    }
    public function getList($map,$order,$limit){
        $UserMoneyLogModel = UserMoneyLogModel::where($map)->order($order)->field('*')->limit($limit);
        $datas = collection($UserMoneyLogModel->with("getLitestoreBy")->select())->toArray();
//        foreach($datas as &$v){
//            $v['distance'] = (double)getdistance(config('config.lng'),config('config.lng'),$v['longitude'],$v['latitude']);
//            $v['categorename'] = $v["get_shop_by_category"]["name"];
//            foreach($v["get_shop_by_tag"] as &$vvv){
//                $v['tag_list'][]=[
//                    'id'=>$vvv['id'],
//                    'tagname'=>$vvv['tagname']
//                ];
//            }
//            unset($v['get_shop_by_tag']);
//            unset($v['get_shop_by_posid_mid']);
//            unset($v['get_shop_by_category']);
//        }
        return $datas;
    }

    /**
     * 我的分红4  我的贡献值 8
     * @param $where
     * @param $offset
     * @param $limit
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getmyPonit($where,$limit,$page){
        $data['total'] = $this->model->where($where)->sum('money');
        $data['list'] = $this->model->where($where)
            ->field('id,money,memo,createtime,shoporder_id,money_type,user_id')
            ->order('createtime desc')
            ->limit($limit)
            ->page($page)
            ->select();

        foreach ($data['list'] as &$val){
            if($val->shoporder_id){
                $shoporder=Db::name('litestore_shoporder')->where(['shoporder_id'=>array('eq',$val['shoporder_id'])])->find();
                if($shoporder){
                    $val['playUser_id']=$shoporder['playUser_id'];
                }else{
                    $val['playUser_id']=0;
                }
            }else{
                $val['playUser_id']=0;
            }
            $val['money_type']=(int)$val['money_type'];
            if($val['money_type']==1){
                $val['memo']='二级个权分润';
                $val['str']='+';
            }elseif($val['money_type']==2){
                $val['memo']='商家分润';
                $val['str']='+';
            }elseif($val['money_type']==3){
                $val['memo']='服务商分润';
                $val['str']='+';
            }elseif($val['money_type']==4){
                $val['memo']='分红';
                $val['str']='+';
            }elseif($val['money_type']==5){
                $val['memo']='股权';
                $val['money']='+'.$val['money'];
            }elseif($val['money_type']==6){
                $val['memo']='充值';
                $val['money']='+'.$val['money'];
            }elseif($val['money_type']==7){
                $userdep=new \app\common\model\UserDeposit();

                $userlai=$userdep->where(['moneylog_id'=>$val['id']])->find();
                if(!empty($userlai)){
                    if($userlai->status==1){
                        $val['status']='审核中';
                    }elseif($userlai->status==2){
                        $val['status']='审核通过';
                    }elseif ($userlai->status==3){
                        $val['status']='审核失败';
                    }
                }


                $val['memo']='提现';
                $val['str']='-';
            }elseif($val['money_type']==8){
                if($val['user_id']==$val['playUser_id']){
                    $val['memo']='消费贡献值';
                    $val['str']='+';
                }else{
                    $val['memo']='二级贡献值';
                    $val['str']='+';
                }

            }elseif($val['money_type']==9){
                $val['memo']='消费余额';
                $val['str']='-';
            }elseif($val['money_type']==10){
                $val['memo']='商家应收款';
                $val['str']='+';
            }elseif($val['money_type']==11){
                $val['money']=$val['money'];
                $val['memo']='二级个权分佣';
            }elseif($val['money_type']==12){
                $val['str']='-';
                $val['memo']='消费金额';
            }elseif($val['money_type']==13){
                $val['str']='+';
                $val['memo']='跨商家收益';
            }
        }
        if(empty($data['list'])){
            $res['msg']="暂无数据";
            $data['list'] = [];
            $res['data']=$data;
            $res['code']=0;
        }else{
            $res['msg']="请求数据正常";
            $res['data']=$data;
            $res['code']=1;
        }
        return $res;
    }

    /**
     * 商家统计数据获取
     * @param $type 类型1=收益记录,2=提现记录,3=消费记录,4=营业记录
     * @param $uid  用户id
     * @param $day  日期格式：2019-05-02
     * @param $offsets 页码
     * @param $limit 一页加载条数默认10
     * @return mixed 返回处理结果
     */
    public function getFinaData($type,$uid,$day,$endDate,$week,$monthDay,$year,$offsets,$limit){
        try{
            $where['ml.user_id']=$uid;
            switch ($type){
                case 1:
                    $where['ml.money_type']=array('in',array('1','2','3','4','5','10','11','13'));
                    break;
                case 3:
                    $where['ml.money_type']=array('in',array('12'));
                    break;
                case 4:
                    $where['ml.money_type']=array('in',array('10'));
                    break;
            }

            //判断如传递年为空就默认当前年
            if(empty($year)){
                $year=date("Y", time()); //年
            }

            //获取指定周开始时间和结束时间
            if(!empty($week)&&!empty($year)){
                $weekArray=weekday($year,$week);
            }else{
                $weekArray=[];
            }

            //获取指定月份开始时间戳和结束时间戳
            if(!empty($year)&&!empty($monthDay)){
                $monthArray=monthday($year,$monthDay);
            }else{
                $monthArray=[];
            }

            if(!empty($date)&empty($endDate)){
                //查询日期
                $dateime=strtotime($date);
                $where2['createtime']=array('between',array($dateime,($dateime+1*24*60*60-1)));
                $where['ml.createtime']=array('between',array($dateime,($dateime+1*24*60*60-1)));
            }elseif(!empty($date)&!empty($endDate)){
                //查询带有开始时间和结束时间
                $dateime=strtotime($date);
                $endDate=strtotime($endDate);
                $where2['createtime']=array('between',array($dateime,$endDate));
                $where['ml.createtime']=array('between',array($dateime,$endDate));
            }elseif(!empty($weekArray)){
                //查询指定周
                $where2['createtime']=array('between',array($weekArray['start'],$weekArray['end']));
                $where['ml.createtime']=array('between',array($monthArray['start'],$monthArray['end']));
            }elseif(!empty($monthArray)){
                //查询指定月
                $where2['createtime']=array('between',array($monthArray['start'],$monthArray['end']));
                $where['ml.createtime']=array('between',array($monthArray['start'],$monthArray['end']));
            }else{
                if(empty($monthDay)){
                    $monthDay=date('m');
                }
                $monthArray=monthday($year,$monthDay);
                $where2['createtime']=array('between',array($monthArray['start'],$monthArray['end']));
                $where['ml.createtime']=array('between',array($monthArray['start'],$monthArray['end']));

            }



            $usermoney=$this->model
                ->alias('ml')
                ->join('litestore_shoporder o','ml.shoporder_id=o.shoporder_id','LEFT')
                ->field('ml.*,o.playUser_id')
                ->where($where)
                ->page($offsets)
                ->limit($limit)
                ->order('ml.createtime desc')
                ->limit(10)
                ->select();

            $usermoney_sum= $this->model
                    ->alias('ml')
                    ->join('litestore_shoporder o','ml.shoporder_id=o.shoporder_id','LEFT')
                    ->field('ml.*,o.playUser_id')
                    ->where($where)
                    ->sum('money');
            if($type==2){
                $wherem['user_id']=array('eq',$uid);
                $wherem1['status']=array('in',array('1','2'));
                $usermoney=Db::name('user_deposit')
                    ->where($wherem)
                    ->where($where2)
                    ->field('*,7 as money_type')
                    ->page($offsets)
                    ->order('createtime desc')
                    ->limit($limit)
                    ->select();
                $user=Db::name('user')->where(['id'=>array('eq',$uid)])->find();
                $usermoney_sum=$user['money'];


                foreach ($usermoney as $key=>$val){
                    if($val['status']==1){
                        $usermoney[$key]['compellation']='审核中';
                    }elseif($val['status']==2){
                        $usermoney[$key]['compellation']='审核通过';
                    }elseif($val['status']==3){
                        $usermoney[$key]['compellation']='审核失败';
                    }
                }

            }
            foreach ($usermoney as $key=>&$val){
                if($val['createtime']){
                    $usermoney[$key]['createtime']=date('Y-m-d H:i',$val['createtime']);
                }

                if($val['money_type']==1){
                    $val['memo']='二级个权分润';
                    $val['str']='+';
                }elseif($val['money_type']==2){
                    $val['memo']='商家分润';
                    $val['str']='+';
                }elseif($val['money_type']==3){
                    $val['memo']='服务商分润';
                    $val['str']='+';
                }elseif($val['money_type']==4){
                    $val['memo']='分红';
                    $val['str']='+';
                }elseif($val['money_type']==5){
                    $val['memo']='股权';
                    $val['money']='+'.$val['money'];
                }elseif($val['money_type']==6){
                    $val['memo']='充值';
                    $val['money']='+'.$val['money'];
                }elseif($val['money_type']==7){

                    $userdep=new \app\common\model\UserDeposit();

                    $userlai=$userdep->where(['moneylog_id'=>$val['id']])->find();
                    if(!empty($userlai)){
                        if($userlai->status==1){
                            $val['compellation']='审核中';
                        }elseif($userlai->status==2){
                            $val['compellation']='审核通过';
                        }elseif ($userlai->status==3){
                            $val['compellation']='审核失败';
                        }
                    }

                    $val['memo']='提现';
                    $val['str']='-';
                }elseif($val['money_type']==8){
                    if($val['user_id']==$val['playUser_id']){
                        $val['memo']='消费贡献值';
                        $val['str']='+';
                    }else{
                        $val['memo']='二级贡献值';
                        $val['str']='+';
                    }
                }elseif($val['money_type']==9){
                    $val['memo']='消费余额';
                    $val['str']='-';
                }elseif($val['money_type']==10){
                    $val['memo']='商家应收款';
                    $val['str']='+';
                }elseif($val['money_type']==11){
                    $val['money']=$val['money'];
                    $val['memo']='二级个权分佣';
                }elseif($val['money_type']==12){
                    $val['str']='-';
                    $val['memo']='消费金额';
                }elseif($val['money_type']==13){
                    $val['str']='+';
                    $val['memo']='跨商家收益';
                }

            }
            if(empty($usermoney)){
                $res['msg']="暂无数据";
                $data['list'] = [];
                $res['data']=$data;
                $res['code']=0;
            }else{
                $data['list']=$usermoney;
                $data['noney']=$usermoney_sum;
                $res['msg']='请求数据正常';
                $res['data']=$data;
                $res['code']=1;
            }

        }catch (\Exception $e){
            $res['msg']="暂无数据";
            $data['list'] = [];
            $res['data']=$e->getMessage();
            $res['code']=0;
        }
        return $res;
    }



}